package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.Inv0004New.Inv0004INWHDto;
import com.ruoyi.buy.domain.entity.Inv0004New.Inv0004NewDto;
import com.ruoyi.buy.domain.entity.PO.BuyPurchaseBill;
import com.ruoyi.buy.domain.entity.PO.BuyPurchaseBillItem;
import com.ruoyi.buy.domain.entity.PO.InvInboundBill;
import com.ruoyi.buy.domain.entity.PO.InvInboundBillItem;
import com.ruoyi.buy.domain.entity.buy0005.Buy0005InvDataDto;
import com.ruoyi.buy.domain.entity.buy0005.Buy0005PostDataDto;
import com.ruoyi.buy.mapper.PO.BuyPurchaseBillItemMapper;
import com.ruoyi.buy.mapper.PO.BuyPurchaseBillMapper;
import com.ruoyi.buy.mapper.PO.Inv0004New.Inv0004NewMapper;
import com.ruoyi.buy.mapper.PO.InvInboundBillItemMapper;
import com.ruoyi.buy.mapper.PO.InvInboundBillMapper;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.buy.service.IInv0004NewService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 参数配置 服务层实现
 * 
 * @author ruoyi
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class Inv0004NewServiceImpl implements IInv0004NewService
{
    @Resource
    Inv0004NewMapper inv0004NewMapper;

    @Resource
    private BuyPurchaseBillMapper buyPurchaseBillMapper;

    @Resource
    private ICommonService commonService;

    @Resource
    private InvInboundBillMapper invInboundBillMapper;

    @Resource
    private BuyPurchaseBillItemMapper buyPurchaseBillItemMapper;

    @Resource
    private InvInboundBillItemMapper invInboundBillItemMapper;

    @Override
    public List<Inv0004NewDto> selectInv0004NewBillList(Inv0004NewDto inv0004NewDto) {
        List<Inv0004NewDto> list = inv0004NewMapper.selectInv0004NewBillList(inv0004NewDto);
        return list;
    }

    @Override
    public List<Inv0004NewDto> selectInv0004NewBillList1(Inv0004NewDto inv0004NewDto) {
        List<Inv0004NewDto> list = inv0004NewMapper.selectInv0004NewBillList1(inv0004NewDto);
        return list;
    }


    @Override
    public int executeIn(Inv0004INWHDto para) throws Exception {
        //查合同相关信息
        BuyPurchaseBill buyPurchaseBill = new BuyPurchaseBill();
        buyPurchaseBill.setvBillNo(para.getvBillNo());
        List<BuyPurchaseBill> buyPurchaseBills = this.buyPurchaseBillMapper.selectBuyPurchaseBillList(buyPurchaseBill);
        buyPurchaseBill = buyPurchaseBills.get(0);
        BuyPurchaseBillItem buyPurchaseBillItem = new BuyPurchaseBillItem();
        buyPurchaseBillItem.setnMainId(buyPurchaseBills.get(0).getId());
        List<BuyPurchaseBillItem> buyPurchaseBillItems = this.buyPurchaseBillItemMapper.selectBuyPurchaseBillItemList(buyPurchaseBillItem);

        //补全主表信息
        InvInboundBill invInboundBill = new InvInboundBill();
        String vBillNo = commonService.getNo("RK");
        invInboundBill.setvBillNo(vBillNo);
        invInboundBill.setIotype("I");
        invInboundBill.setvType("ICG");
//        invInboundBill.setvCorBillNo(bill.getvBillNo());
        invInboundBill.setdExecute(DateUtils.getNowDate());
        invInboundBill.setvContractNo(buyPurchaseBill.getvBillNo());//合同号
        invInboundBillMapper.insertInvInboundBill(invInboundBill);

        //反查主表ID
        invInboundBill.setdExecute(null);
        List<InvInboundBill> list = this.invInboundBillMapper.selectInvInboundBillList(invInboundBill);
        Long nMainId = list.get(0).getId();

        //补全子表信息
        InvInboundBillItem item = new InvInboundBillItem();
        item.setnMainId(nMainId);
        List<BuyPurchaseBillItem> result = buyPurchaseBillItems.stream().filter(x ->  x.getvSpaName().equals(para.getvSpaName())
                && x.getvSpaBrand().equals(para.getvSpaBrand()) && x.getvSpaBrand().equals(para.getvSpaBrand())).collect(Collectors.toList());
        item.setnPrice(result.get(0).getnPrice());
        item.setvProjectNo(buyPurchaseBill.getvProjectNo());
        item.setvProjectDec(buyPurchaseBill.getvProjectDec());
        item.setvSpaName(para.getvSpaName());
        item.setvSpaModel(para.getvSpaModel());
        item.setvSpaBrand(para.getvSpaBrand());
        item.setnQty(para.getnThisINWHQty());
        item.setvWHId(String.valueOf(para.getvWHId()));
        item.setvLocationId(String.valueOf(para.getvLocationId()));
        item.setvAreaId(String.valueOf(para.getvAreaId()));
        this.invInboundBillItemMapper.insertInvInboundBillItem(item);

        //再更新已入库数量
        if (result.get(0).getnINWHQty() == null){
            result.get(0).setnINWHQty(para.getnThisINWHQty());
        }else{
            result.get(0).setnINWHQty(para.getnThisINWHQty().add(result.get(0).getnINWHQty()));
        }
        this.buyPurchaseBillItemMapper.updateBuyPurchaseBillItem(result.get(0));

        //反查明细
        InvInboundBillItem searchItem = new InvInboundBillItem();
        searchItem.setnMainId(nMainId);
        List<InvInboundBillItem> listResult = this.invInboundBillItemMapper.selectInvInboundBillItemList(searchItem);
        // 更改库存
        commonService.changeInventory(list.get(0), new ArrayList<>(listResult),"IN");
        return 1;
    }

    @Override
    public List<Buy0005PostDataDto> getPostData(Buy0005PostDataDto para) {
        List<Buy0005PostDataDto> list = inv0004NewMapper.getPostData(para);
        return list;
    }

    @Override
    public List<Buy0005InvDataDto> getInvData(Buy0005InvDataDto para) {
        List<Buy0005InvDataDto> list = inv0004NewMapper.getInvData(para);
        return list;
    }
}
